<?php

/***************商品规格****************/
/*******2013-4-30********/

class GoodspropAction extends Action
{
	public function preMethod()
	{
		R("Mayijie/check_admin");
		$perm_key = session('admin_perm');
		$perm_arr = explode(",",$perm_key);
		if(!in_array(4,$perm_arr)){
			$this->error("无权操作");
		}
	}

	public function index(){
		$Prop=M('Goods_prop');
		$this->props=$Prop->group('prop_name')->select();
		$this->display();
	}

	public function edit(){
		$Prop=M('Goods_prop');
		$prop=$this->_get('prop_name');
		$map['prop_name']=urldecode($prop);
		$this->prop_value=$Prop->where($map)->select();
        $this->assign('prop',$prop);
		$this->display();
	}

	public function save_edit(){
		$Prop=M('Goods_prop');
		$prop_id=$this->_post('prop_id');
		$prop_value=$this->_post('prop_value');

		for($i=0;$i<count($prop_id);$i++){
			$data['prop_value']=$prop_value[$i];
			$Prop->where('prop_id='.$prop_id[$i])->save($data);
		}
	}

	public function add(){
		$Prop=M('Goods_prop');
		$name=$this->_post('prop_name');
		$prop_value=$this->_post('prop_value');
		//检查是否为空
		if(empty($name)||empty($prop_value)){
			$this->error("不得为空！");
		}

		$arr=explode(chr(13),$prop_value);
		$value_key=str_replace("\\n","",json_encode($arr));
		$value_key=json_decode($value_key);

		$data['prop_name']=$name;
		for($i=0;$i<count($value_key);$i++){
			$data['prop_value']=$value_key[$i];
			$result=$Prop->add($data);
			if($result){
				$this->success("添加规格成功！");
			}else{
				$this->error("添加失败！");
			}
		}
	}
	/*添加规格结束*/
    
	/*删除整个规格，包括规格值 ！@@注意prop表的特殊结构！*/
	public function del(){
		$Prop=M('Goods_prop');
		$map['prop_name']=$this->_post('name');
		if($Prop->where($map)->delete()){
			echo '1';
		}else{
			echo '0';
		}
	}

	//删除单个规格值
	public function del_value(){
		$Prop=M('Goods_prop');
		$map['prop_id']=$this->_get('id');
		if($Prop->where($map)->delete()){
			echo '1';
		}else{
			echo '0';
		}
	}

	public function get_prop(){
		$Prop=M('Goods_prop');
		return $Prop->group('prop_name')->select();
	}

    //添加商品时根据选择生成所有规格
	public function prop_value(){
		$Prop=M('Goods_prop');
		$goods_prop=$this->_post('goods_prop');
		$prop_num=count($goods_prop);

		if($prop_num==2){
			$map['prop_name']=$goods_prop[0];
		    $map2['prop_name']=$goods_prop[1];

		    $a=$Prop->where($map)->select();
		    $b=$Prop->where($map2)->select();

			//数据库中操作：先取出某一规格，然后循环合并另一规格，成为一个新的数组
		    //@@@
			for($i=0;$i<count($a);$i++){
				for($j=0;$j<count($b);$j++){
					$new[]=array('k1'=>$a[$i]['prop_value'],'k2'=>$b[$j]['prop_value']);
				}
			}
			//@输出规格项，如：“颜色、尺寸”  名称
			$this->assign("name1",$a[0]['prop_name']);
			$this->assign("name2",$b[0]['prop_name']);
			$this->assign("prop",$new);
			
		}else if($prop_num==1){
			$map['prop_name']=$goods_prop[0];
			$a=$Prop->where($map)->select();
			$this->assign("prop",$a);
			$this->assign("name1",$a[0]['prop_name']);
		}

        $this->assign("prop_num",$prop_num);
		$this->price=$this->_post('price');
		$this->store=$this->_post('store');
		$this->display();
	}
	/*获取规格值end*/
 

}
?>